package com.attendance.controller;

import com.attendance.common.Result;
import com.attendance.dto.request.AttRuleRequest;
import com.attendance.entity.AttRule;
import com.attendance.service.AttRuleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/att-rule")
public class AttRuleController {

    @Autowired
    private AttRuleService attRuleService;

    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> createRule(@Validated @RequestBody AttRuleRequest request) {
        attRuleService.createRule(request);
        return Result.success();
    }

    @PutMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> updateRule(@Validated @RequestBody AttRuleRequest request) {
        attRuleService.updateRule(request);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> deleteRule(@PathVariable Long id) {
        attRuleService.deleteRule(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<AttRule> getRule(@PathVariable Long id) {
        return Result.success(attRuleService.getRule(id));
    }

    @GetMapping("/page")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Page<AttRule>> pageRules(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String ruleName) {
        return Result.success(attRuleService.pageRules(pageNum, pageSize, ruleName));
    }

    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        attRuleService.updateStatus(id, status);
        return Result.success();
    }
} 